package com.cl.spark.udf;

import com.cl.spark.util.AddressUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.spark.sql.api.java.UDF1;

// 定义 UDF 类,最小单位地址解析
@Slf4j
public class AddressParseUDF implements UDF1<String, String> {
    @Override
    public String call(String value) {
        try {

            if (value == null) {
                return "null";
            }
            AddressUtil.AddressInfo addressInfo = AddressUtil.addressFormat(value);
            String res = addressInfo.getVillage();


            if (res.isEmpty()) {
                if (!addressInfo.getCity().isEmpty()) {
                    res = addressInfo.getCity();
                } else if (!addressInfo.getProvince().isEmpty()) {
                    res = addressInfo.getProvince();
                }
            }
            if (res.contains("null") || res.isEmpty()) {
                res = addressInfo.getDetail();
            }
            return res;

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return "error";
        }
    }
}
